import {AbstractWebSocket} from "../AbstractWebSocket";

import {uuid} from "../../../../utils/uuid.js";

export class XingHuoSocket extends AbstractWebSocket {

    constructor(url, appId, version, callBack) {
        super(url);
        this.appId = appId;
        this.version = version;
        this.callBack = callBack;
    }


    getDomain() {
        switch (this.version) {
            case "v3.1":
                return "generalv3"
            case "v2.1":
                return "generalv2"
            default:
                return "general"
        }
    }

    send(message) {

        let historyJson = localStorage.getItem(this.appId);
        if (!historyJson) {
            historyJson = [];
        } else {
            historyJson = JSON.parse(historyJson);
        }
        const object = {
            "header": {
                "app_id": this.appId,
                "uid": uuid(),
            },
            "parameter": {
                "chat": {
                    "domain": this.getDomain(),
                    "temperature": 0.5,
                    "max_tokens": 2048,
                }
            },
            "payload": {
                "message": {
                    "text": [
                        // {"role": "user", "content": "你会做什么"}
                    ]
                }
            }
        }

        if (historyJson) {
            object.payload.message.text = historyJson;
        }

        object.payload.message.text.push(
            {role: "user", content: message}
        )

        //1tokens 约等于1.5个中文汉字 或者 0.8个英文单词 8192
        console.log("星火大模型，入参是:", object);
        this.param = object.payload.message.text;
        localStorage.setItem(this.appId, JSON.stringify(object.payload.message.text));
        super.send(JSON.stringify(object));
    }

    clearHistory() {
        localStorage.setItem(this.appId, null);
    }

    getHistory() {
        var b = localStorage.getItem(this.appId);
        return JSON.parse(b);
    }

    onMessage(e) {
        const data = e.data;
        // message data format https://www.xfyun.cn/doc/spark/Web.html#_1-%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
        const message = JSON.parse(data);
        console.log("星火大模型，出参是:", message);

        const result = message.payload.choices.text;

        let historyJson = localStorage.getItem(this.appId);
        if (!historyJson) {
            historyJson = [];
        } else {
            historyJson = JSON.parse(historyJson);
        }
        const status = message.payload.choices.status;

        let allMessage = "";
        result.forEach(v => {
            allMessage += v.content;
        });

        //开始
        if (status == 0) {
            this.result = {};
            this.result.role = result[0].role;
            this.result.content = allMessage;
            //中间
        } else if (status == 1) {
            this.result.content = this.result.content + allMessage;
            //结束
        } else if (status == 2) {
            this.result.content = this.result.content + allMessage;
            historyJson.push(this.result);
            localStorage.setItem(this.appId, JSON.stringify(historyJson));
        }

        result.forEach((v) => {
            const text = v.content;
            // console.log(message)
            if (text) {
                for (let i = 0; i < text.length; i++) {
                    const c = text.charAt(i);
                    if ((i == 0 || i == text.length - 1) && c === '"') continue
                    this.callBack(c, status);
                }
            }
        })

    }
}